//
//  LabelTextScroll.m
//  LabelTextScroll
//
//  Created by  on 12-9-9.
//  Copyright (c) 2012年  All rights reserved.
//

#import "MarqueeLabel.h"
#import <QuartzCore/QuartzCore.h>

@interface MarqueeLabel()

@property (retain, nonatomic) UILabel *insideLabel;

@end

@implementation MarqueeLabel

@synthesize scrollSpeed;
@synthesize insideLabel;

- (void)setText:(NSString *)theText
{
    insideLabel.text=theText;
    NSLog(@"%.1f. %.1f", insideLabel.frame.size.width, insideLabel.frame.size.height);
    [self setLabelTextScroll:NO scrollSpeed:60.f];}
-(void)setFont:(UIFont *)theFont
{
    insideLabel.font=theFont;
}

- (id)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if(self)
    {
        insideLabel = [[UILabel alloc]init];
        
        // [self setFrame:CGRectMake(20, 180, 280, 60)];
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        
        //  [self setFrame:CGRectMake(20, 180, 280, 60)];
        insideLabel = [[UILabel alloc]init];
        insideLabel.text = self.text;
        insideLabel.font = self.font;
        insideLabel.textColor = self.textColor;
        self.textColor = [UIColor clearColor];
        [self setLabelTextScroll:NO scrollSpeed:60.0f];
    }
    return self;
}



-(void)setLabelTextScroll:(BOOL)ScrollWhetherReverse scrollSpeed:(float)speed
{
    insideLabel.backgroundColor=[UIColor clearColor];
    CGSize textSize= [insideLabel.text sizeWithFont:insideLabel.font constrainedToSize:CGSizeMake(9999,self.frame.size.height) lineBreakMode:UILineBreakModeWordWrap];
    
    [UIView beginAnimations:@"testAnimation" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationRepeatAutoreverses:ScrollWhetherReverse];
    
    if (textSize.width > self.frame.size.width)
    {
        [insideLabel setFrame:CGRectMake(0,0,textSize.width,self.frame.size.height)];
        // insideLabel.textColor=[UIColor blackColor];
        [self addSubview:insideLabel]; 
        float textDuration;
        
        scrollSpeed =speed;
        textDuration =(insideLabel.frame.size.width+self.frame.size.width )/ scrollSpeed;
        CGRect frame = insideLabel.frame;
        frame.origin.x = self.frame.size.width;
        insideLabel.frame = frame;
        
        [UIView setAnimationDuration:textDuration];
        [UIView setAnimationRepeatCount:HUGE_VALF];
        
        frame = insideLabel.frame;
        frame.origin.x = -(insideLabel.frame.size.width);
        insideLabel.frame = frame;
    }
    else
    {
        [UIView setAnimationDuration:0];
        [UIView setAnimationRepeatCount:0];
        
        [insideLabel setFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
        insideLabel.textAlignment=self.textAlignment;
        // insideLabel.textColor=[UIColor blackColor];
        [self addSubview:insideLabel]; 
    }
    [UIView commitAnimations];
}

-(void)dealloc{
    SAFE_RELEASE(insideLabel);
    [super dealloc];
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */
@end
